//
//  HTAddOrderLineViewController.m
//  TruckDriver
//
//  Created by Ting Han on 2017/9/27.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTAddOrderLineViewController.h"
#import "HTAddOrderCell.h"
#import "HTAddOrderLineCell.h"
#import "HTPlaceView.h"

#import "HTTruckLengthTypeVC.h"
#import "HTMLocationManager.h"

@interface HTAddOrderLineViewController ()<UITableViewDataSource,UITableViewDelegate,HTTruckLengthTypeVCDelegate,HTPlaceViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSArray *headData;
@property (nonatomic,strong) NSMutableArray *footData;

@property (nonatomic,strong) HTPlaceView *placeView;

@property (nonatomic,strong) NSString *currentCity;


@property (nonatomic,strong) NSString *startAddress;
@property (nonatomic,strong) NSString *destinateAddress;
@property (nonatomic,strong) NSString *lengthString;
@property (nonatomic,strong) NSString *truckTypeString;

@end

@implementation HTAddOrderLineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadLocation];
    [self configTable];
   
}
#pragma mark --- 定位
- (void)loadLocation{
    if ([[HTMLocationManager singleInstance] getCurrentAddress].length <= 0) {
        //定位请求
        [self requestLocation];
        self.currentCity = @"请选择";
    }else{
        self.currentCity  = [[HTMLocationManager singleInstance] getCurrentAddress];
    }
    self.startAddress =self.currentCity ;
}
- (void)requestLocation{
    [[HTMLocationManager singleInstance]requestLocationCompletionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
        if (error)
        {
            if (error.code == AMapLocationErrorLocateFailed)
            {
                [HTShowUtil showMessage:@"定位失败"];
                return;
            }
        }
        if (regeocode)
        {
           
            HTAddOrderLineCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
            if (regeocode.city == nil || regeocode.city.length <= 0) {
                self.currentCity = @"北京";
                cell.secondLabel.text = self.currentCity;
                return;
            }
            [HTMLocationManager singleInstance].currentCity = regeocode.city;
            [HTMLocationManager singleInstance].currentProvince = regeocode.province;
             cell.secondLabel.text =  [[HTMLocationManager singleInstance]getCurrentAddress];
            self.startAddress =  cell.secondLabel.text;
        }
    }];
}

- (void)configTable{
    self.title = @"添加订阅路线";
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = kBackgroundColor;
    [self.tableView registerNib:[UINib nibWithNibName:@"HTAddOrderCell" bundle:nil] forCellReuseIdentifier:addOrderCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"HTAddOrderLineCell" bundle:nil] forCellReuseIdentifier:addOrderLineCell];
    self.headData = @[@"出发地",@"目的地",@"车长车型"];
    self.footData = [NSMutableArray arrayWithCapacity:3];
    [self.footData addObject:self.currentCity];
    [self.footData addObject:@"请选择"];
    [self.footData addObject:@"不限"];
    self.truckTypeString = @"";
    self.lengthString = @"0";
}
//切换数据
- (void)reloadTable:(NSInteger)count withText:(NSString *)text{
   HTAddOrderLineCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:count inSection:0]];
   cell.secondLabel.text = text;
}
#pragma mark --- tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 1) {
        return 1;
    }
    return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        return kFooterHeight;
    }
    return kWidth(60);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        HTAddOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:addOrderCell forIndexPath:indexPath];
        [cell.addBtn setTitle:@"添加" forState:UIControlStateNormal];
        [cell.addBtn addTarget:self action:@selector(didClickCommit:) forControlEvents:UIControlEventTouchUpInside];
        cell.backgroundColor = kBackgroundColor;
        return cell;
    }else{
        HTAddOrderLineCell *cell = [tableView dequeueReusableCellWithIdentifier:addOrderLineCell forIndexPath:indexPath];
        cell.startLabel.text = self.headData[indexPath.row];
        cell.secondLabel.text = self.footData[indexPath.row];
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 2) {
        HTTruckLengthTypeVC *vc = [[HTTruckLengthTypeVC alloc]init];
        vc.delegate = self;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (indexPath.section == 0 && indexPath.row == 0) {
        //出发地
        [self setupPlaceView];
        self.placeView.placeViewType = HTPlaceViewTypeStart;
        HTAddOrderLineCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.isChosed = YES;
    }
    if (indexPath.section == 0 && indexPath.row == 1) {
        //目的地
        [self setupPlaceView];
        self.placeView.placeViewType = HTPlaceViewTypeDestination;
        HTAddOrderLineCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.isChosed = YES;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:tableView.tableHeaderView.bounds];
    view.backgroundColor = kBackgroundColor;
    return view;
}
#pragma mark --- 车长车型
- (void)didChoiceLength:(NSString *)length type:(NSString *)truckType{
    self.lengthString = length;
    self.truckTypeString = truckType;
    if ([length isEqualToString:@"不限"]) {
        self.lengthString = @"0";
    }
    if ([truckType isEqualToString:@"不限"]) {
        self.truckTypeString = @"";
    }
    NSString *lengthType = @"";
    if ([length isEqualToString:@"不限"] && [truckType isEqualToString:@"不限"]) {
        lengthType = @"不限";
    }else if(![length isEqualToString:@"不限"]){
        lengthType = [NSString stringWithFormat:@"%@米",length];
        if (![truckType isEqualToString:@"不限"]) {
            lengthType = [NSString stringWithFormat:@"%@/%@",lengthType,truckType];
        }
    }else{
        lengthType = truckType;
    }
   HTAddOrderLineCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
   cell.secondLabel.text = lengthType;
    
}
#pragma mark --- 出发地目的地
- (void)setupPlaceView{
    if (!self.placeView) {
        self.placeView = [[HTPlaceView alloc]initWithFrame:CGRectMake(0, 0, kHTScreenWidth, kHTScreenHeight - kHTNavicationBarAndStatusBar)];
        self.placeView.delegate = self;
        __weak typeof (self)weakSelf = self;
        self.placeView.blockCallBack = ^{
            [weakSelf removePlaceView];
        };
        [self.view addSubview:self.placeView];
    }
}
- (void)removePlaceView{
    if (self.placeView) {
        [self.placeView removeFromSuperview];
        if (self.placeView.placeViewType == HTPlaceViewTypeStart) {
          HTAddOrderLineCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
            cell.isChosed = NO;
        }else{
            HTAddOrderLineCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
            cell.isChosed = NO;
        }
        self.placeView = nil;
    }
}
- (void)didChoiceItemWithModel:(HTAreaModel *)areaModel index:(NSInteger)index PlaceViewType:(HTPlaceViewType)placeType{
    if (areaModel.areaType == HTAreaTypeNo || areaModel.areaType == HTAreaTypeArea) {
        
        if (placeType == HTPlaceViewTypeStart) {
            self.startAddress = areaModel.title;
            HTAddOrderLineCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
            cell.secondLabel.text = areaModel.title;
            cell.isChosed = NO;
        }else if(placeType == HTPlaceViewTypeDestination){
            self.destinateAddress = areaModel.title;
            HTAddOrderLineCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
            cell.secondLabel.text = areaModel.title;
            cell.isChosed = NO;
        }
         [self removePlaceView];
    }
}
- (void)didClickCommit:(UIButton *)button{
    if (self.startAddress == nil || self.startAddress.length <= 0) {
        [HTShowUtil showMessage:@"请选择出发地"];
        return;
    }
    if (self.destinateAddress == nil || self.destinateAddress.length <= 0) {
        [HTShowUtil showMessage:@"请选择到达地"];
        return;
    }
    [self netLoadData];
}
- (void)netLoadData{
    [self startHud];
    [HTHttpRequestManager requestCreatlinesWithFromAddress:self.startAddress withToAddress:self.destinateAddress withLength:self.lengthString withCartype:self.truckTypeString withIssound:YES success:^(id  _Nullable responseObject) {
        [self endHud];
        DBLog(@"添加订阅线路-----：%@",responseObject);
        if (!responseObject) {
            [HTShowUtil showMessage:@"添加订阅线路失败"];
            return;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            [HTShowUtil showMessage:responseObject[@"message"]];
            return;
        }
        [HTShowUtil showMessage:@"添加订阅线路成功"];
        if (self.delegate && [self.delegate respondsToSelector:@selector(didClickConform)]) {
            [self.delegate didClickConform];
        }
        [self.navigationController popViewControllerAnimated:YES];
        
    } failure:^(NSError * _Nonnull error) {
        [self endHud];
        [HTShowUtil showMessage:@"网络错误"];
    }];
}
@end
